﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ordinaire.Social
{
    /// <summary>
    /// Represents a type of organisation.
    /// </summary>
    public class Organisation : Entity<int>
    {
        #region Variables

        /// <summary>
        /// Organisation name.
        /// </summary>
        private string name;

        /// <summary>
        /// The id of the default address.
        /// </summary>
        private int defaultAddressId;

        /// <summary>
        /// List of associated adddresses.
        /// </summary>
        private Dictionary<string, Address> addresses;

        #endregion

        #region Constructor

        /// <summary>
        /// Returns new instance of Ordinaire.Social.Organisation.
        /// </summary>
        public Organisation()
        {
        }

        /// <summary>
        /// Returns new instance of Ordinaire.Social.Organisation with the specified name.
        /// </summary>
        /// <param name="name"></param>
        public Organisation(string name)
        {
            this.name = name;
        }

        #endregion

        #region Property methods

        /// <summary>
        /// Gets or sets the organisation name.
        /// </summary>
        public override string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }

        /// <summary>
        /// Gets or sets the id of the default address.
        /// </summary>
        public int DefaultAddressId
        {
            get { return this.defaultAddressId; }
            set { this.defaultAddressId = value; }
        }

        /// <summary>
        /// Returns the default address if specified; otherwise the only associated address.
        /// </summary>
        public Address Address
        {
            get
            {
                Address defaultAddress = this.addresses.Values.Where(d => d.Id == this.defaultAddressId).SingleOrDefault();
                if (defaultAddress == null && this.addresses.Count == 1)
                {
                    return this.addresses.Values.SingleOrDefault();
                }
                return defaultAddress;
            }
        }

        /// <summary>
        /// Gets or sets list of associated addresses.
        /// </summary>
        public Dictionary<string, Address> Addresses
        {
            get { return this.addresses; }
            set { this.addresses = value; }
        }

        #endregion
    }
}
